package top.lauwen.xwen;

import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.TabLayout;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private TabLayout mTabTl;
    private ViewPager mContentVp;

    private List<String> tabIndicators;
    private List<Fragment> tabFragments;
    private ContentPagerAdapter contentAdapter;

    /**
     * 播放按钮
     */
    private ImageButton imgBtnNext;
    private ImageButton imgBtnPlay;
//    private ListView list;
//    private ArrayList<Music> musicArrayList;
    //当前歌曲序号，下标从零开始
    private int number = 0;

    public MainActivity() {
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * 搜索按钮
         */
        ImageButton searchBtn = (ImageButton) findViewById(R.id.searchBtn);
        searchBtn.setOnClickListener(new searchBtnListener());

        /**
         * 侧边栏
         */
        ImageButton slidingMenu = (ImageButton) findViewById(R.id.slidingMenu);
        slidingMenu.setOnClickListener(new slidingMenuListener());
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        /**
         * 标签页
         */
        mTabTl = (TabLayout) findViewById(R.id.tl_tab);
        mContentVp = (ViewPager) findViewById(R.id.vp_content);
        initContent();
        initTab();

        /**
         * 播放音乐
         */

        findViews();
        registerListener();
    }
    private void findViews(){
        imgBtnNext = (ImageButton) findViewById(R.id.neBtn);
        imgBtnPlay = (ImageButton) findViewById(R.id.controlBtn);
//        list = (ListView) findViewById(R.id.localMusic);
    }
    /**
     * 为显示组件设置监听器
     */
    private void registerListener(){
        imgBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moveNumberToNext();
//                play(number);
                imgBtnPlay.setBackgroundResource(R.drawable.pause);
            }
        });
        imgBtnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(player != null && player.isPlaying()){
                    pause();
                    imgBtnPlay.setBackgroundResource(R.drawable.play);
                }else{
                    play(number);
                    imgBtnPlay.setBackgroundResource(R.drawable.pause);
                }
            }
        });
    }

    protected void setBackground(){
        imgBtnPlay.setBackgroundResource(R.drawable.pause);
    }
    //媒体播放类
    private MediaPlayer player = new MediaPlayer();

    /**
     * 读取音乐文件
     */
    private void load(int number){
        try{
            player.reset();
            player.setDataSource(MusicList.getMusicList().get(number).getMusicPath());
            player.prepare();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 播放音乐
     */
    protected void play(int number){
        //停止当前播放
        if(player != null && player.isPlaying()){
            player.stop();
        }
        load(number);
        player.start();
    }
    /**
     * 暂停播放
     */
    private void pause(){
        if(player.isPlaying()){
            player.pause();
        }
    }
    /**
     * 停止播放
     */
    private void stop(){
        player.stop();
    }
    /**
     * 恢复播放
     */
    private void resume(){
        player.start();
    }
    /**
     * 重新播放
     */
    private void replay(){
        player.start();
    }
    /**
     * 上一曲
     */
    private void moveNumberToPrevious() {
        //判断是否到达顶端
        if(number == 0){
            Toast.makeText(MainActivity.this, MainActivity.this.getString(R.string.tip_reach_top), Toast.LENGTH_SHORT).show();
        }else{
            --number;
            play(number);
        }
    }

    /**
     * 下一曲
     */
    private void moveNumberToNext(){
        if((number) == (MusicList.getMusicList().size()-1)){
            Toast.makeText(MainActivity.this, MainActivity.this.getString(R.string.tip_reach_bottom), Toast.LENGTH_SHORT).show();
        }else{
            ++number;
            play(number);
        }
    }











    /**
     * 搜索按钮
     */
    class searchBtnListener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            Intent intent  = new Intent();
            intent.setClass(MainActivity.this,SearchActivity.class);
            startActivity(intent);
        }
    }

    /**
     * 侧边栏
     */
    class slidingMenuListener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                drawer.openDrawer(GravityCompat.START);
            }
        }
    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    /**
     * 标签页
     */
    private void initTab(){
        mTabTl.setTabMode(TabLayout.MODE_FIXED);
        mTabTl.setTabTextColors(ContextCompat.getColor(this, R.color.gray), ContextCompat.getColor(this, R.color.white));
        mTabTl.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.colorPrimary));
        ViewCompat.setElevation(mTabTl, 10);
        mTabTl.setupWithViewPager(mContentVp);
    }

    private void initContent(){
        tabIndicators = new ArrayList<>();
        tabIndicators.add("我的音乐");
        tabIndicators.add("在线音乐");

        tabFragments = new ArrayList<>();
        tabFragments.add(TabContentFragment.newInstance());
        tabFragments.add(TabContentFragment1.newInstance());
        contentAdapter = new ContentPagerAdapter(getSupportFragmentManager());
        mContentVp.setAdapter(contentAdapter);
    }

    class ContentPagerAdapter extends FragmentPagerAdapter {

        public ContentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return tabFragments.get(position);
        }

        @Override
        public int getCount() {
            return tabIndicators.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabIndicators.get(position);
        }
    }

}
